# gps - a graphical ps/kill utility

# Dialog resources are included at the end of this file
RES=$(whence $0)

# Here are some constants for the controls in the dialog.
IDOK=1
IDC_KILL=100
IDC_REFRESH=101
IDC_LIST=102

# Make these variables into integers to make math faster
typeset -i first win95 width height dlgwidth dlgheight xpos ypos

# This function fills up the list box with the output from the ps command.
function refresh_list
{
	# Clear out the list box
	dlg clear -c $IDC_LIST

	first=0

	# Read in the output from 'ps' using a here document.
	while IFS=" " read -r pid pri size rss start time command name
	do
		if [ first -eq 0 ]
		then
			# Skip the ps header
			first=1
			continue
		fi

		# 'ps' produces different output on Win95 vs NT, so we need
		# to read it differently.
		if [ win95 -eq 1 ]
		then
			dlg addtext -c $IDC_LIST "$name" "$pid" "$pri" "$size" "$rss" "$start" "$time" "$command"
		else
			dlg addtext -c $IDC_LIST "$command" "$pid" "$start" "$time" "$pri" "$size" "$rss"
		fi

	done <<EOF
$(ps $psflags)
EOF
}

# This function resizes the controls in the results dialog, so when the
# user resized the dialog, the controls move to fill the space.
function resize_results
{
	dlg getpos -w dlgwidth -h dlgheight
	dlg getpos -c $IDC_LIST -x xpos -y ypos -w width -h height
	width=$dlgwidth-125
	height=$dlgheight-50
	dlg setpos -c $IDC_LIST -x $xpos -y $ypos -w $width -h $height
	xpos=$width+30
	dlg setpos -c $IDOK -x $xpos -y $ypos
	ypos=$ypos+30
	dlg setpos -c $IDC_KILL -x $xpos -y $ypos
	ypos=$ypos+30
	dlg setpos -c $IDC_REFRESH -x $xpos -y $ypos
}


# This is where the program really begins.  Load the dialog.
if ! dlg load -x -1 -y -1 $RES DIALOG_1
then
	exit
fi

# ps provides different output on Win95 vs NT, so we need to distinguish
# between the two.
if [ $(uname) = Windows_95 ]
then
	win95=1
	psflags=-l
	dlg columns -c $IDC_LIST Command 100 ID 75 "Parent ID" 75 Count 50 Thread 50 Priority 100 "Module ID" 75 "Heap ID" 75
else
	win95=0
	psflags=-Al
	dlg columns -c $IDC_LIST Command 100 ID 50 Start 75 Time 50 Priority 50 Size 50 Rss 50
fi

# Size the controls to fit the size of the dialog, and fill it up with
# the 'ps' output.
resize_results
refresh_list

# The event loop for the dialog
while dlg event msg control; do
	case $msg in
	command)
		case $control in
		$IDOK)
			# We're done.
			break ;;
		$IDC_REFRESH)
			# Refresh the process list.
			refresh_list ;;
		$IDC_KILL)
			# Kill the selected process.
			dlg getcursel -c $IDC_LIST index
			if [ index -ne -1 ]
			then
				dlg gettext -c $IDC_LIST -i $index command pid
				if [ win95 -eq 1 ]
				then
					# Win95 PIDs are in hex, rather than decimal
					pid="0x"$pid
					kill $pid
				else
					kill $pid
				fi
				# Update the process list.
				refresh_list
			fi ;;
		
		esac ;;

	size)
		# The user resized the frame so resize 
		# our child windows to fit
		resize_results ;;

	esac
done

dlg close

# That's all folks.

# Dialog resources follow this line
#[]mks internal resource : dialog : DIALOG_1
#[ ]begin : size 282
#[  ]MP #,D      $  8 #P L :,      %  <@!O &, 90!S ', ( !3 '0 80!T
#[  ]M '4 <P    @ 30!3 "  4P!A &X <P @ %, 90!R &D 9@         !4   
#[  ]M  #Q  8 ,@ .  $ __^  $, ; !O ', 90         !4     #Q !@ ,@ .
#[  ]M &0 __^  "8 2P!I &P ;  @ %  <@!O &, 90!S ',          5      
#[  ]M\0 K #( #@!E /__@  F %( 90!F '( 90!S &@         !0"!4      )
#[  ]M  < WP"4 &8 4P!Y ', 3 !I ', = !6 &D 90!W #, ,@   $< 90!N &4 
#[  ],<@!I &, ,0      
#[ ]end
#[]mks internal resource : icon : \0 : width 32 height 32 colors 16
#[ ]begin : size 744
#[  ]M*    "    !      0 $      "  @                              
#[  ]M (   (    " @ "     @ "  ("   # P,  @("     _P  _P   /__ /\ 
#[  ]M  #_ /\ __\  /___P     (B(B(B(B(B(B(B(B(                    
#[  ]M"(   #,S,S,S,S,S,S,S,PB(  "[N[N[N[N[NP   /,("(  O[^_O[^_O[^_
#[  ]MO[^S"!B(                   !"(              B(B $!"(        
#[  ]M        @!$!"(,S,S,S,S,S,S,S,( 1$!#_N[N[N[N[N[N[NS" $1$ _[N[
#[  ]MN[N[N[N[N[LP@/$1"P______\     "[,("?$+NP     /_\_/SPNS" &0_[
#[  ]M        _\_/P+LP@ "_\ #_____  _\_/"[,(  "P /     /  _\_ NS" 
#[  ]M   /\ J@"@ /  _\\+LP@   #_"@"@H* / /_\"[,(    _PH H*"@ / /SP
#[  ]MNS"    /\ J@"@"J#P#_P+LP@   #_        \ _/"[,(    _P"@"J * /
#[  ]M /  NS"    /\ H* *"@#P"[N[LP@   #_ *"@"@H \ ^[N[,(    _P"@"J
#[  ]M * / /___S"    /\       \ #P        #_________            #_
#[  ]M______\                                                     
#[  ]M                                         /X   #X    >    #@ 
#[  ]M   8    "    0?   $    !     0    $    !     0    $    !    
#[  ]M <    '@   !X    <    '    !P    <    '    !P    <    '    #
#[  ]8X  '_^  !__@  __^  ?__P /___ /__
#[ ]end
#[]mks internal resource : icon : \1 : width 16 height 16 colors 16
#[ ]begin : size 296
#[  ]M*    !     @     0 $      #                                 
#[  ]M (   (    " @ "     @ "  ("   # P,  @("     _P  _P   /__ /\ 
#[  ]M  #_ /\ __\  /___P           ( +N[N[N[NP"          0@ ,S,S,S
#[  ]M !$(       ($0@S,[N[NPB0L   #\S+")L J@H _,L( /"@"J /RP@ \*H*
#[  ]M"@_+" #P    #\L( / *"@H/NP@ \*H*J@\   #P          ____      
#[  ]M         ,  F 1          !@#      ,                 @    (  
#[  ]:  "     @    ( !  " 'P8 P#\  .!_  "?
#[ ]end
